package org.abc.fund.repository.factorRepository;

import org.abc.fund.entity.factorEntity.StyleFactor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 风格因子Repository
 */
@Repository
public interface StyleFactorRepository extends JpaRepository<StyleFactor, Long> {

    /**
     * 根据因子代码查找风格因子
     */
    Optional<StyleFactor> findByFactorCode(String factorCode);

    /**
     * 根据风格类型查找风格因子列表
     */
    List<StyleFactor> findByStyleType(StyleFactor.StyleType styleType);

    /**
     * 根据是否启用查找风格因子列表
     */
    List<StyleFactor> findByEnabled(Boolean enabled);

    /**
     * 统计启用的风格因子数量
     */
    long countByEnabledTrue();

    /**
     * 根据计算频率查找风格因子列表
     */
    List<StyleFactor> findByFrequency(StyleFactor.CalculationFrequency frequency);

    /**
     * 根据风格类型和是否启用查找风格因子列表
     */
    List<StyleFactor> findByStyleTypeAndEnabled(StyleFactor.StyleType styleType, Boolean enabled);

    /**
     * 根据因子名称模糊查询
     */
    @Query("SELECT sf FROM StyleFactor sf WHERE sf.factorName LIKE %:factorName%")
    List<StyleFactor> findByFactorNameContaining(@Param("factorName") String factorName);

    /**
     * 根据因子代码模糊查询
     */
    @Query("SELECT sf FROM StyleFactor sf WHERE sf.factorCode LIKE %:factorCode%")
    List<StyleFactor> findByFactorCodeContaining(@Param("factorCode") String factorCode);

    /**
     * 分页查询风格因子
     */
    Page<StyleFactor> findByStyleTypeAndEnabled(StyleFactor.StyleType styleType, Boolean enabled, Pageable pageable);

    /**
     * 检查因子代码是否存在
     */
    boolean existsByFactorCode(String factorCode);

    /**
     * 根据创建人查找风格因子列表
     */
    List<StyleFactor> findByCreatedBy(String createdBy);

    /**
     * 多条件搜索风格因子
     */
    @Query("SELECT sf FROM StyleFactor sf WHERE " +
           "(:name IS NULL OR sf.factorName LIKE %:name%) AND " +
           "(:code IS NULL OR sf.factorCode LIKE %:code%) AND " +
           "(:styleType IS NULL OR sf.styleType = :styleType) AND " +
           "(:algorithm IS NULL OR sf.formula LIKE %:algorithm%) AND " +
           "(:enabled IS NULL OR sf.enabled = :enabled)")
    Page<StyleFactor> searchStyleFactors(@Param("name") String name,
                                        @Param("code") String code,
                                        @Param("styleType") StyleFactor.StyleType styleType,
                                        @Param("algorithm") String algorithm,
                                        @Param("enabled") Boolean enabled,
                                        Pageable pageable);

    Page<StyleFactor> findByStyleTypeAndEnabled(String styleType, Boolean enabled, Pageable pageable);
    Page<StyleFactor> findByStyleType(String styleType, Pageable pageable);
    Page<StyleFactor> findByEnabled(Boolean enabled, Pageable pageable);

}